package com.project;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ServerThread implements Runnable {

    private static String header = "HTTP/1.1 200 OK\r\n" +
            "Content-Type: text/html;charSet=utf-8\r\n" +
            "\r\n";
    private Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
        new Thread(this).start();

    }

    @Override
    public void run() {

        try {
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();
            Request req = new Request(in);
            Response res = new Response(out);
            System.out.println(req);

            String str = req.getSul();

            out.write(header.getBytes());
            res.sendFile(str);
            out.flush();

            out.close();
            in.close();
            socket.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
